﻿using System.Windows;
using System.Windows.Controls;
using Dispatcher.ViewModels;
using Repository;

namespace Dispatcher.Views
{
    /// <summary>
    /// Interaction logic for AddEventDialog.xaml
    /// </summary>
    public partial class EventEditControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="EventEditControl"/> class.
        /// </summary>
        public EventEditControl()
        {
            InitializeComponent();

            if (MainWindow.CurrentUser == null)
            {
                Close();
                return;
            }

            if (MainWindow.CurrentUser.Role.Name == "User")
            {
                Organazer.Visibility = Visibility.Collapsed;
                Combo2.Visibility = Visibility.Collapsed;
            }
        }

        /// <summary>
        /// Handles the Checked event of the CheckBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            var viewModel = (EventEditViewModel)DataContext;
            var checkBox = (CheckBox)sender;

            if (checkBox.DataContext is EventGroup)
            {
                PlaceSearchView.Check(checkBox, viewModel.SelectedWeeks);
            }
        }

        /// <summary>
        /// Handles the Click event of the CancedlButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void CancedlButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        /// <summary>
        /// Handles the Click event of the OkButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            var viewModel = (EventEditViewModel)DataContext;

            if (viewModel.Validate())
            {
                viewModel.Save();
                DialogResult = true;
                Close();
            }
        }
    }
}
